package cn.itrip.auth.service.itripuser;
import cn.itrip.auth.exception.UserLoginFailedException;
import cn.itrip.common.MD5;
import cn.itrip.dao.itripuser.ItripUserMapper;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

import cn.itrip.common.Constants;
@Service
public class ItripUserServiceImpl implements ItripUserService {

    @Resource
    private ItripUserMapper itripUserMapper;

    public ItripUser getItripUserById(Long id)throws Exception{
        return itripUserMapper.getItripUserById(id);
    }

    public List<ItripUser>	getItripUserListByMap(Map<String,Object> param)throws Exception{
        return itripUserMapper.getItripUserListByMap(param);
    }

    public Integer getItripUserCountByMap(Map<String,Object> param)throws Exception{
        return itripUserMapper.getItripUserCountByMap(param);
    }

    public Integer itriptxAddItripUser(ItripUser itripUser)throws Exception{
            itripUser.setCreationDate(new Date());
            return itripUserMapper.insertItripUser(itripUser);
    }

    public Integer itriptxModifyItripUser(ItripUser itripUser)throws Exception{
        itripUser.setModifyDate(new Date());
        return itripUserMapper.updateItripUser(itripUser);
    }

    public Integer itriptxDeleteItripUserById(Long id)throws Exception{
        return itripUserMapper.deleteItripUserById(id);
    }

    public Page<ItripUser> queryItripUserPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize)throws Exception{
        Integer total = itripUserMapper.getItripUserCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ItripUser> itripUserList = itripUserMapper.getItripUserListByMap(param);
        page.setRows(itripUserList);
        return page;
    }

    @Override
    public ItripUser login(String name, String password) throws Exception {
        // 判断用户名
        Map<String,Object> param = new HashMap<>();
        param.put("userCode",name);
        List<ItripUser> itripUsers = itripUserMapper.getItripUserListByMap(param);
        if(itripUsers.isEmpty()){
           throw new UserLoginFailedException("用户不存在!");
        }
        // 判断密码
        ItripUser itripUser = itripUsers.get(0);
        String md5Password = MD5.getMd5(password, 32);
        if(!itripUser.getUserPassword().equals(md5Password)) {
            throw new UserLoginFailedException("密码不正确!");
        }
        // 判断是否激活
        if(itripUser.getActivated()== Constants.USER_UNACTIVATED) {
            throw new UserLoginFailedException("用户未激活!");
        }
        // 返回结果
        return itripUser;
    }

}
